package com.mercury.coffee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mercury.coffee.entity.CoffeeInfoVo;
import com.mercury.coffee.entity.CoffeeTypeVo;
import com.mercury.coffee.entity.PushCoffeeType;
import com.mercury.coffee.mapper.PushCoffeeTypeMapper;
import com.mercury.coffee.service.PushCoffeeTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 童雅婷
 * @since 2021-11-07
 */
@Service
public class PushCoffeeTypeServiceImpl extends ServiceImpl<PushCoffeeTypeMapper, PushCoffeeType> implements PushCoffeeTypeService {

    /**
     * @date 2021/11/7 20:16
     * @params coffeeLabel
     * @return
     * @description 根据标签 id 获得该标签的名称
    */
    public String selectNameById(Integer coffeeLabel) {
        PushCoffeeType coffeeType = baseMapper.selectById(coffeeLabel);
        return coffeeType.getTypeName();
    }

    /**
     * @date 2021/11/10 11:51
     * @params
     * @return
     * @description 首先获得所有的父类型，然后再获得所有的子类新
    */
    @Override
    public List<CoffeeTypeVo> getByGroup() {
        List<CoffeeTypeVo> typeGroup = new ArrayList<>();
        QueryWrapper<PushCoffeeType> wrapper = new QueryWrapper<>();
        wrapper.isNull("type_parent_id");
        List<PushCoffeeType> typeList = baseMapper.selectList(wrapper);
        for (PushCoffeeType pushCoffeeType : typeList) {
            CoffeeTypeVo coffeeTypeVo = new CoffeeTypeVo();
            QueryWrapper<PushCoffeeType> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("type_parent_id", pushCoffeeType.getTypeId());
            List<PushCoffeeType> sonTypeList = baseMapper.selectList(wrapper2);
            coffeeTypeVo.setParentType(pushCoffeeType);
            coffeeTypeVo.setSonTypes(sonTypeList);
            typeGroup.add(coffeeTypeVo);
        }
        return typeGroup;
    }
}
